package com.itheima.mall.api.pms;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mall.dto.ProductCategoryDto;
import com.itheima.mall.exception.BusinessException;
import com.itheima.mall.mapper.pms.ProductAttributeMapper;
import com.itheima.mall.mapper.pms.ProductCategoryAttributeRelationMapper;
import com.itheima.mall.mapper.pms.ProductCategoryMapper;
import com.itheima.mall.mapper.pms.ProductMapper;
import com.itheima.mall.pojo.pms.Product;
import com.itheima.mall.pojo.pms.ProductAttribute;
import com.itheima.mall.pojo.pms.ProductCategory;
import com.itheima.mall.pojo.pms.ProductCategoryAttributeRelation;
import com.itheima.mall.vo.pms.ProductCategoryChildrenVo;
import com.itheima.mall.vo.pms.ProductCategoryVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

@DubboService
@Slf4j
public class ProductCategoryServiceApiImpl implements ProductCategoryServiceApi {
    @Autowired
    private ProductCategoryMapper pmsProductCategoryMapper;

    @Autowired
    private ProductCategoryAttributeRelationMapper productCategoryAttributeRelationMapper;
    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductAttributeMapper productAttributeMapper;

    //根据id获取商品分类
    @Override
    public ProductCategory getProduct(Integer id) {
        Long pid = Convert.toLong(id);
        LambdaQueryWrapper<ProductCategory>wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(ProductCategory::getId,pid);
        return pmsProductCategoryMapper.selectOne(wrapper);
    }

    //修改商品分类
    @Override
    public void updateProduct(ProductCategory productCategory, Integer id) {
        Long pid = Convert.toLong(id);
        LambdaQueryWrapper<ProductCategory>wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(ProductCategory::getId,pid);
        pmsProductCategoryMapper.update(productCategory,wrapper);
    }
    //
    @Override
    public void updateEcho(ProductCategory productCategory, Long[] ids) {

    }


    //修改导航栏显示状态
    @Override
    public void updateNavStatus(Integer navStatus, Long[] ids) {
        for (Long id : ids) {
            ProductCategory productCategory=new ProductCategory();
            productCategory.setId(id);
            productCategory.setNavStatus(navStatus);
            pmsProductCategoryMapper.updateById(productCategory);
        }
    }


    //分页查询商品分类
    @Override
    public ProductCategoryVo selectPaging(Integer parentId, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<ProductCategory>wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(parentId!=null,ProductCategory::getParentId,parentId);
        wrapper.orderByDesc(ProductCategory::getSort);
        Page pageInfo=new Page(pageNum,pageSize);
        Page page = pmsProductCategoryMapper.selectPage(pageInfo, wrapper);
        ProductCategoryVo productCategoryVo=new ProductCategoryVo();
        productCategoryVo.setList(page.getRecords());
        productCategoryVo.setPageSize(Convert.toInt(page.getSize()));
        productCategoryVo.setPageNum((Convert.toInt(page.getCurrent())));
        productCategoryVo.setTotalPage((Convert.toInt(page.getPages())));
        productCategoryVo.setTotal(Convert.toInt(page.getTotal()));
        return productCategoryVo;
    }

    //修改显示状态
    @Override
    public void updateshowStatus(Integer showStatus, Long[] ids) {
        for (Long id : ids) {
            ProductCategory productCategory=new ProductCategory();
            productCategory.setId(id);
            productCategory.setShowStatus(showStatus);
            pmsProductCategoryMapper.updateById(productCategory);
        }
    }

    //查询所有一级分类及子分类
    @Override
    public List<ProductCategoryChildrenVo> withChildren() {
        LambdaQueryWrapper<ProductCategory>wrapper=new LambdaQueryWrapper<>();
        List<ProductCategory> productCategories = pmsProductCategoryMapper.selectList(wrapper);//查询到所有的数据

        List<ProductCategoryChildrenVo> vo=new ArrayList<>();

        for (ProductCategory productCategory : productCategories) {//父
            List<ProductCategory> childVo = new ArrayList<>();
            for (ProductCategory category : productCategories) {//子
                if(productCategory.getId() == category.getParentId()){
                    //查询其子菜单
                    ProductCategory category1 = pmsProductCategoryMapper.selectById(category.getId());
                    childVo.add(category1);
                }
            }
            //创建ProductCategoryChildrenVo对象
            ProductCategoryChildrenVo productCategoryChildrenVo = new ProductCategoryChildrenVo();
            //将child菜单set到productCategoryChildrenVo
            productCategoryChildrenVo.setChildren(childVo);
            //给父菜单赋值
            BeanUtil.copyProperties(productCategory,productCategoryChildrenVo);
            //加入到父菜单的集合中
            vo.add(productCategoryChildrenVo);
        }
        for (ProductCategoryChildrenVo productCategoryChildrenVo : vo) {
            log.error("productCategoryChildrenVo:{}",productCategoryChildrenVo);
        }
        log.error("size:{}",vo.size());
        return vo;
    }

    @Override
    public void create(ProductCategoryDto productCategoryDto) {
        LambdaQueryWrapper<ProductCategory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductCategory::getName,productCategoryDto.getName());
        wrapper.eq(ProductCategory::getParentId,Convert.toLong(productCategoryDto.getParentId()));
        ProductCategory selectOne = pmsProductCategoryMapper.selectOne(wrapper);
        if (selectOne != null){
            throw new BusinessException("商品分类已存在,添加失败!");
        }
        ProductCategory productCategory = new ProductCategory();
        BeanUtil.copyProperties(productCategoryDto,productCategory);
        productCategory.setParentId(Convert.toLong(productCategoryDto.getParentId()));
        if (productCategoryDto.getParentId()==0){
            productCategory.setLevel(0);
        }else {
            productCategory.setLevel(1);
        }
        productCategory.setProductCount(100);
        pmsProductCategoryMapper.insert(productCategory);
        List<Integer> productAttributeIdList = productCategoryDto.getProductAttributeIdList();
        for (Integer productAttributeId : productAttributeIdList) {
            ProductCategoryAttributeRelation productCategoryAttributeRelation = new ProductCategoryAttributeRelation();
            productCategoryAttributeRelation.setProductCategoryId(productCategory.getId());
            productCategoryAttributeRelation.setProductAttributeId(Convert.toLong(productAttributeId));
            productCategoryAttributeRelationMapper.insert(productCategoryAttributeRelation);
            ProductAttribute productAttribute = productAttributeMapper.selectById(productAttributeId);
            productAttribute.setRelatedStatus(1);
            productAttributeMapper.updateById(productAttribute);
        }
    }

    @Override
    public Boolean deleteById(Long productCategoryId) {
        Boolean flag = true;
        LambdaQueryWrapper<Product> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Product::getProductCategoryId,productCategoryId);
        List<Product> products = productMapper.selectList(queryWrapper1);
        if(products.size() != 0){
            flag = false;
        }else {
            LambdaQueryWrapper<ProductCategoryAttributeRelation> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ProductCategoryAttributeRelation::getProductCategoryId,productCategoryId);
            productCategoryAttributeRelationMapper.delete(queryWrapper);
            LambdaQueryWrapper<ProductCategory> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ProductCategory::getId,productCategoryId);
            pmsProductCategoryMapper.delete(wrapper);
        }
        return flag;
    }

}
